﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Threading;

namespace XDA_ROM_Hub
{
    public partial class StartupConfirmation : Form
    {
        public StartupConfirmation()
        {
            InitializeComponent();
        }

        private void StartupConfirmation_Load(object sender, EventArgs e)
        {
                Properties.Settings.Default.Reset();
                if (File.Exists(Application.StartupPath + @"\build\build.prop"))
                {
                    File.Delete(Application.StartupPath + @"\build\build.prop");
                }
                Get_Device_Info();

                Device_ID();
            
        }

        private void button2_Click(object sender, EventArgs e)
        {
            Welcome w = new Welcome();
            w.Show();
            this.Hide();
            
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Properties.Settings.Default.Reset();
            if (File.Exists(Application.StartupPath + @"\build\build.prop"))
            {
                File.Delete(Application.StartupPath + @"\build\build.prop");
            }
                Get_Device_Info();
                
                //
                //Get_Android_Version();
            Device_ID();
        }
    





    public void Get_Device_Info()
        {
            System.Diagnostics.ProcessStartInfo p = new System.Diagnostics.ProcessStartInfo(Application.StartupPath + @"\checkbuild.bat");
            System.Diagnostics.Process proc = new System.Diagnostics.Process();
            
            proc.StartInfo = p;
            p.CreateNoWindow=true;
            
            proc.Start();
            proc.WaitForExit();
            //
        }

        private string DeviceID = "Unknown";
        private string AndroidVer = "Unckown";
        public void Device_ID()
        {
            try
            {
                Get_Android_Version();
                Get_Device_ID();
            }
            catch
            {
                if (MessageBox.Show("Please connect your device and enable USB Debbuging\nTo enable USB Debugging go to Options->Developer options->Enable USB Debugging.", "Device Not Found", MessageBoxButtons.RetryCancel, MessageBoxIcon.Exclamation) == DialogResult.Retry)
                {
                    this.Device_ID();
                }
            }


        }
        private void Get_Android_Version()
        {
            this.Get_Device_Info();
            string str2 = "ro.build.version.release=";
            TextReader reader = new StreamReader(Application.StartupPath + @"\build\build.prop");
            string str3 = reader.ReadToEnd();

                if (str3.Contains(str2 + "4.1.1"))
                {
                    AndroidVer = "4.1.1 Jelly Bean";
                }
                else if (str3.Contains(str2 + "4.1"))
                {
                    AndroidVer = "4.1 Jelly Bean";
                }
                else if (str3.Contains(str2 + "4.0.4"))
                {
                    AndroidVer = "4.0.4 Ice Cream Sandwich";
                }
                else if (str3.Contains(str2 + "4.0.3"))
                {
                    AndroidVer = "4.0.3 Ice Cream Sandwich";
                }
                else if (str3.Contains(str2 + "2.3.7"))
                {
                    AndroidVer = "2.3.7 Gingerbread";
                }
                else if (str3.Contains(str2 + "2.3.3"))
                {
                    AndroidVer = "2.3.3 Gingerbread";
                }
                else if (str3.Contains(str2 + "2.3"))
                {
                    AndroidVer = "2.3 Gingerbread";
                }

                else
                {
                    AndroidVer = "Unknown";
                }
                

            reader.Close();
        }
        private void Get_Device_ID()
        {
            this.Get_Device_Info();
            string str = "ro.product.name=";
            string str2 = "ro.build.product=";
            TextReader reader = new StreamReader(Application.StartupPath + @"\build\build.prop");
            string str3 = reader.ReadToEnd();
            if (str3.Contains(str2) || str3.Contains(str))
            {
                if (str3.Contains(str2 + "WT19a"))
                {
                    DeviceID = "Xperia Live with Walkman (WT19a)";
                    this.message();
                }
                if (str3.Contains(str2 + "ST17a"))
                {
                    DeviceID = "Xperia Active (ST17a)";
                    this.message();
                }
                else if (str3.Contains(str2 + "ST15i"))
                {
                    DeviceID = "Xperia Mini (ST15i)";
                    this.message();
                }
                else if (str3.Contains(str2 + "SK17i"))
                {
                    DeviceID = "Xperia Mini Pro (SK17i)";
                    this.message();
                }
                else if (str3.Contains(str2 + "SK17a"))
                {
                    DeviceID = "Xperia Mini Pro (SK15a)";
                    this.message();
                }
                else if (str3.Contains(str2 + "ST17i"))
                {
                    DeviceID = "Xperia Active (ST17i)";
                    this.message();
                }
                else if (str3.Contains(str2 + "WT19i"))
                {
                    DeviceID = "Xperia Live with Walkman (WT19i)";
                    this.message();
                }
                else if (str3.Contains(str2 + "ST18i"))
                {
                    DeviceID = "Xperia Ray (ST18i)";
                    this.message();
                }
                else if (str3.Contains(str2 + "LT15i"))
                {
                    DeviceID = "Xperia Arc (LT15i)";
                    this.message();
                }
                else if (str3.Contains(str2 + "LT18i"))
                {
                    DeviceID = "Xperia Arc S (LT18i)";
                    this.message();
                }
                else if (str3.Contains(str2 + "MT15i"))
                {
                    DeviceID = "Xperia Neo (MT15i)";
                    this.message();
                }
                else if (str3.Contains(str2 + "MT11i"))
                {
                    DeviceID = "Xperia Neo V (MT11i)";
                    this.message();
                }
                else if (str3.Contains(str2 + "R800i"))
                {
                    DeviceID = "Xperia Play (R800i)";
                    this.message();
                }
                else if (str3.Contains(str2 + "MK16i"))
                {
                    DeviceID = "Xperia Pro (MK16i)";
                    this.message();
                }
                
                else if (str3.Contains(str + "WT19a"))
                {
                    DeviceID = "Xperia Live with Walkman (WT19a)";
                    this.message();
                }
                else if (str3.Contains(str + "ST17a"))
                {
                    DeviceID = "Xperia Active (ST17a)";
                    this.message();
                }
                else if (str3.Contains(str + "ST15i"))
                {
                    DeviceID = "Xperia Mini (ST15i)";
                    this.message();
                }
                else if (str3.Contains(str + "SK17i"))
                {
                    DeviceID = "Xperia Mini Pro (SK17i)";
                    this.message();
                }
                else if (str3.Contains(str + "SK17a"))
                {
                    DeviceID = "Xperia Mini Pro (SK15a)";
                    this.message();
                }
                else if (str3.Contains(str + "ST17i"))
                {
                    DeviceID = "Xperia Active (ST17i)";
                    this.message();
                }
                else if (str3.Contains(str + "WT19i"))
                {
                    DeviceID = "Xperia Live with Walkman (WT19i)";
                    this.message();
                }
                else if (str3.Contains(str + "ST18i"))
                {
                    DeviceID = "Xperia Ray (ST18i)";
                    this.message();
                }
                else if (str3.Contains(str + "LT15i"))
                {
                    DeviceID = "Xperia Arc (LT15i)";
                    this.message();
                }
                else if (str3.Contains(str + "LT18i"))
                {
                    DeviceID = "Xperia Arc S (LT18i)";
                    this.message();
                }
                else if (str3.Contains(str + "MT15i"))
                {
                    DeviceID = "Xperia Neo (MT15i)";
                    this.message();
                }
                else if (str3.Contains(str + "MT11i"))
                {
                    DeviceID = "Xperia Neo V (MT11i)";
                    this.message();
                }
                else if (str3.Contains(str + "R800i"))
                {
                    DeviceID = "Xperia Play (R800i)";
                    this.message();
                }
                else if (str3.Contains(str + "MK16i"))
                {
                    DeviceID = "Xperia Pro (MK16i)";
                    this.message();
                }
                
            }

            this.message();
            reader.Close();
        }

        //string DeviceID;
        private void message()
        {
            Properties.Settings.Default.SelectedDevice = DeviceID;
            Properties.Settings.Default.AndroidVer = AndroidVer;
            label2.Text = "Name: " + Properties.Settings.Default.SelectedDevice;
            label3.Text = "Android Version: " + Properties.Settings.Default.AndroidVer;
            button1.Enabled = true;
            button2.Enabled = true;
            button2.Select();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }
    }
}
